package com.admin.business.user;

import com.admin.bo.ReturnEnum;
import com.tiangong.framework.BusinessBus;
import com.tiangong.framework.annotation.*;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @description: 用户表
 * @create: 2021-08-17 17:19:32
 **/
@Service("demo")
@Slf4j
@OpenApi
public class DemoService {

    /**
     * 返回非正常状态码
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return demo1(CDO cdoRequest) {
        return Return.valueOf(ReturnEnum.DEMO_TEXT_1);
    }

    /**
     * redis缓存 必须有redis，缓存的是整个方法执行的结果集,默认300s,返回的数据长度为空，不进行缓存
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return demo2(CDO cdoRequest) {
        log.info("未经过缓存");
        CDO cdoResult=new CDO();
        cdoResult.set("demo","demo2");
        return Return.valueOf(cdoResult);
    }

    /**
     * redis缓存 必须有redis，缓存的是整个方法执行的结果集,默认300s
     * @param cdoRequest
     * @return
     */
    @OpenApi
    @ServiceFilter(checkHeader = true,checkToken = true,minVersion = "v2",versionForward = "demo4")
    public Return demo3(CDO cdoRequest) {
        log.info("最低版本v2");
        return Return.SUCCESS;
    }

    @OpenApi
    public Return demo4(CDO cdoRequest) {
        log.info("最低版本v1");
        return Return.SUCCESS;
    }

    /**
     * 内部调用，任何位置都可以使用
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return demo5(CDO cdoRequest) {
        //调用本service服务
        CDO cdoReq=CDO.newRequest(BusinessBus.getServiceName(this.getClass()),"demo6");
        return BusinessBus.handleService(cdoReq);
    }

    @OpenApi
    public Return demo6(CDO cdoRequest) {
        CDO cdoResult=new CDO();
        cdoResult.set("demo","demo6");
        return Return.valueOf(cdoResult);
    }

    /**
     * 异步调用其他方法
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return demo7(CDO cdoRequest) {
        //调用本service服务
        CDO cdoReq=CDO.newRequest(BusinessBus.getServiceName(this.getClass()),"demo8");
        cdoReq.set("testValue","demo7");
        BusinessBus.handleEventService(cdoReq);
        return Return.SUCCESS;
    }

    @OpenApi
    public Return demo8(CDO cdoRequest) {
        log.info(cdoRequest.getString("testValue"));
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        CDO cdoResult=new CDO();
        cdoResult.set("demo","demo8");
        return Return.valueOf(cdoResult);
    }

    /**
     * 前置触发器
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return demo9(CDO cdoRequest) {
        log.info("demo9");
        return Return.SUCCESS;
    }

    /**
     * common触发器,type = Trigger.PRETRIGGER/Trigger.POSTTRIGGER  前置/后置
     * @param cdoRequest
     * @return
     */
    @Trigger(args = "testValue", method = "demo.demo8",type = Trigger.PRETRIGGER)
    public Return demo12(CDO cdoRequest) {
        log.info("demo12");
        return Return.SUCCESS;
    }

    /**
     * common触发器,type = Trigger.PRETRIGGER/Trigger.POSTTRIGGER  前置/后置
     * @param cdoRequest
     * @return
     */
    @Trigger(args = "testValue", method = "demo.demo8",type = Trigger.POSTTRIGGER)
    public Return demo13(CDO cdoRequest) {
        log.info("demo13");
        return Return.SUCCESS;
    }

    /**
     * common异步触发器,type = Trigger.PRETRIGGER/Trigger.POSTTRIGGER  前置/后置
     * @param cdoRequest
     * @return
     */
    @Trigger(args = "testValue", method = "demo.demo8",type = Trigger.PRETRIGGER,async = true)
    public Return demo14(CDO cdoRequest) {
        log.info("demo14");
        return Return.SUCCESS;
    }

    /**
     * 禁止对外访问,@OpenApi注解到类上是全局生效，注解到方法上只对当前方法生效
     * @param cdoRequest
     * @return
     */
    @OpenApi(OpenApi.REFUSE)
    public Return demo15(CDO cdoRequest) {
        log.info("demo15");
        return Return.SUCCESS;
    }

}
